package com.situ.allaia.controller;

import com.situ.allaia.interceptor.LoginInterceptor;
import com.situ.allaia.model.AdminUser;
import com.situ.allaia.service.AdminUserService;
import com.wf.captcha.SpecCaptcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Controller
@RequestMapping("/admin_user")
public class AdminUserController {

    private final AdminUserService adminUserService;

    public AdminUserController(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    @GetMapping("/login")
    public String login(){
        return "login-1";
    }

    @PostMapping(value="/login",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> login(AdminUser adminUser,String captcha,HttpServletRequest request){
        if (!StringUtils.hasText(adminUser.getUsername())){
            return Map.of("success",false,"error","用户名不可为空");
        }
        if (!StringUtils.hasText(adminUser.getPassword())){
            return Map.of("success",false,"error","密码不可为空");
        }

        String correct = (String) request.getSession().getAttribute("captcha");
        if (!correct.equals(captcha)){
            return Map.of("success",false,"error","验证码不正确");
        }

        AdminUser user = adminUserService.findByUsername(adminUser.getUsername());
        if (user==null){
            return Map.of("success",false,"error","用户不存在");
        }else {
            String encrypt = adminUser.getPassword()+"{"+adminUser.getUsername()+"}";
            encrypt = DigestUtils.md5DigestAsHex(encrypt.getBytes(StandardCharsets.UTF_8));
            if (!user.getPassword().equals(encrypt)){
                return Map.of("success",false,"error","密码不正确");
            }

        }

        request.getSession().setAttribute(LoginInterceptor.CURRENT_ADMIN_LOGIN_USER_KEY,user);

        return Map.of("success",true);
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request ){
        request.getSession().removeAttribute(LoginInterceptor.CURRENT_ADMIN_LOGIN_USER_KEY);
        return "redirect:"+ request.getContextPath()+"/index";
    }

    @GetMapping("/captcha")
    public void  captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0L);

        HttpSession session = request.getSession();

        SpecCaptcha captcha = new SpecCaptcha(110, 38, 4);

        session.setAttribute("captcha", captcha.text().toLowerCase());
        captcha.out(response.getOutputStream());
    }


    @GetMapping("/list")
    public String list(){
        return "admin_user/list";
    }
    @GetMapping("/add")
    public String add(){
        return "admin_user/add";
    }
    @GetMapping("/edit")
    public String edit(Integer id,Map<String,Object> map){
        AdminUser adminUser = adminUserService.findById(id);
        if (adminUser==null){
            map.put("error","要修改的用户不存在");
        }else{
            map.put("user",adminUser);
        }
        return "admin_user/edit";
    }

}
